package com.zhx.threadcoreknowledge.threadobjectclasscommonmethod;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-03 11:52
 * 用途         ： 证明wait只释放当前的那把锁
 */
public class WaitNotifyReleaseOwnMonitor {

    private static Object resourceA = new Object();

    private static Object resourceB = new Object();


    static class Thread1 extends Thread{
        @Override
        public void run() {
            synchronized (resourceA){
                System.out.println(Thread.currentThread().getState()+"拿到第一把锁");
                synchronized (resourceB){
                    System.out.println(Thread.currentThread().getState()+"拿到第二把锁");
                    try {
                        resourceA.wait();
                        System.out.println(Thread.currentThread().getState()+"释放了第一把锁");
                    } catch (InterruptedException e) {

                    }
                }
            }
        }
    }
    
    static class Thread2 extends Thread{
        @Override
        public void run() {
            synchronized (resourceA){
                System.out.println(Thread.currentThread().getState()+"拿到第一把锁");
                synchronized (resourceB){
                    System.out.println(Thread.currentThread().getState()+"拿到第二把锁");
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        Thread1 thread2 = new Thread1();

        thread1.start();
        thread2.start();
    }

}
